package org.aom.litebean.module.security;

import java.util.Map;

import org.aom.entity.security.Role;
import org.aom.litebean.module.base.BaseEntityFormBean;
import org.aom.service.security.IRoleManager;
import org.aom.util.FaceUtil;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.ManagedProperty;
import org.operamasks.faces.annotation.Required;
@ManagedBean(name = "module.security.roleFormBean", scope = ManagedBeanScope.REQUEST)
public class RoleFormBean extends BaseEntityFormBean{
	
	@Bind(id = "name", attribute = "value")
    @Required(message = "#{this.getMessages().get('required')}")
	private String name;
	
	private Role role;
	
	@ManagedProperty("#{roleManager}")
	private IRoleManager roleManager;

	@Override
	@Action(id="ok")
	protected void ok() {
		Map<String, Object> session = FaceUtil.getSessionMap();
		if(session.get(ACTION_NAME).toString().equals("add")){
			role = new Role();
		}else {
			selectedModuleId = (Long)session.get(SELECTED_MODULE_ID);
			role = roleManager.get(selectedModuleId);
		}
		role.setName(name);
		roleManager.save(role);
		cancel();
	}

	@Override
	protected void processCancel() {
		name = "";
	}
	
	@BeforeRender
	protected void beforeRender(boolean isPostBack) {
		initBaseResource();	
		Map<String, Object> session = FaceUtil.getSessionMap();
		if (session.get(ACTION_NAME).toString().equals("mod")) {
			selectedModuleId = (Long)session.get(SELECTED_MODULE_ID);
			role = roleManager.get(selectedModuleId);
			name = role.getName();
		}
	}

}
